import api from '../../../api/index';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    pageNum: 0,
    pageSize: 5000,
    imagelogo: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/community/moren.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getlist()
  },
  getlist(name='') {
    const actions = {
      data: {
        name
      },
      head: {
        firstresult: this.data.pageNum,
        maxresults: this.data.pageSize
      }
    };
    api.post('/api/hxhealth/music/list', actions).then(res => {
    console.log(res);
    this.setData({
      list: res
    })
  }).catch(e => {
      console.log(e)
      wx.showToast({
        icon: 'error',
        duration: 1200,
        title: e.faultstring,
      })
  })
  },
  onChange(e) {
    console.log(e);
    this.setData({
      value: e.detail,
    });
  },
  onSearch() {
    console.log('搜索' + this.data.value);
    this.getlist(this.data.value)
  },
  playMusic(e) {
    console.log(e.target.dataset.item.file);
    let file = e.target.dataset.item.file
    let title = e.target.dataset.item.name
    this.playMusicstartfile(file,title)
  },
  playMusicstartfile(file,title=''){
    wx.playBackgroundAudio({
      dataUrl: file,
      title: title,
      coverImgUrl: '音乐封面URL'
    });
  },
  changemusic (e) {
    let file = e.target.dataset.item.file
    let title = e.target.dataset.item.name
    let logo =  e.target.dataset.item.logo
    if(!logo) {
      logo = 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/community/moren.png'
      wx.setStorageSync('logo', logo);
    }
    wx.setStorageSync('file', file);
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
    
    // wx.navigateTo({ url: `/community/pages/addpic/index?title=${title}&file=${file}&logo=${logo}` });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})